package cn.com.utt.unms.mapper;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import cn.com.utt.unms.pojo.WXAuthorizer;
import cn.com.utt.unms.pojo.WifiConf;

@Mapper
public interface WifiCfgMapper {

	@Select("select * from wx_authorizer where cfg_id=#{cfgId}")
	WXAuthorizer queryWxAuthorizer(@Param("cfgId") long cfgId);

	@Select("select * from wifi_cfg where id=#{id}")
	WifiConf queryWifiConfById(@Param("id") long id);
	
	@Select("select * from wifi_cfg where user_id=#{userId}")
	List<WifiConf> queryAll(@Param("userId") long userId);

	@Insert("insert into `wifi_cfg`(user_id,name,access_limit,access_type,wx_token,wx_sid,sim_enable,wx_auth_type,free_rx_limit,free_tm_limit,concurrent_limit,phone_rx_limit,phone_tm_limit,wx_rx_limit,wx_tm_limit,account_rx_limit,account_tm_limit) values(#{userId},#{name},#{accessLimit},#{accessType},#{wxToken},#{wxSid},#{simEnable},#{wxAuthType},#{freeRxLimit},#{freeTmLimit},#{concurrentLimit},#{phoneRxLimit},#{phoneTmLimit},#{wxRxLimit},#{wxTmLimit},#{accountRxLimit},#{accountTmLimit})")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
	void save(WifiConf conf);
	
	@Update("update `wifi_cfg` set access_limit=#{accessLimit},concurrent_limit=#{concurrentLimit},access_type=#{accessType},wx_auth_type=#{wxAuthType},free_tm_limit=#{freeTmLimit},free_rx_limit=#{freeRxLimit},phone_tm_limit=#{phoneTmLimit},phone_rx_limit=#{phoneRxLimit},sim_enable=#{simEnable},wx_account=#{wxAccount},wx_pc_free=#{wxPcFree},wx_tm_limit=#{wxTmLimit},wx_rx_limit=#{wxRxLimit},wx_sacn_code=#{wxSacnCode},semacode_path=#{semacodePath},wx_sid=#{wxSid},wx_token=#{wxToken},wx_ssid=#{wxSsid},wx_store_id=#{wxStoreId},mp_app_id=#{mpAppId},mp_app_secret=#{mpAppSecret},need_attention=#{needAttention},account_tm_limit=#{accountTmLimit},account_rx_limit=#{accountRxLimit},pl_id=#{plId},page_dir=#{pageDir},name=#{name} where id=#{id}")
	void update(WifiConf conf);
	
	@Select({"<script>",
	    "SELECT COUNT(*) AS total", 
	    "FROM wifi_cfg",
	    "WHERE ",
	    "user_id = #{userId}", 
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	    "</script>"})
	int countByCondition(Map<String, Object> conditions);
	
	@Select({"<script>",
	    "SELECT *", 
	    "FROM wifi_cfg",
	    "WHERE ",
	    "user_id = #{userId}", 
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	      "<if test='orderColumn != null'>",
	      "order by ${orderColumn} ${orderDirection}",
	      "</if>",
	      "limit #{start},#{rows}",
	    "</script>"})
	List<WifiConf> queryByCondition(Map<String, Object> conditions);
	
	@Delete({"<script>",
	    "delete ", 
	    "FROM wifi_cfg",
	    "WHERE ", 
	    "id IN", 
	      "<foreach item='item' index='index' collection='cfgIds'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	void delete(@Param("cfgIds")Collection<Long> cfgIds);

}